"""
Compliance Item 合规项表模型
"""
import uuid
from datetime import datetime
from sqlalchemy import Column, Text, Enum, DateTime, Integer, ForeignKey
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship
from db.base import Base

class ComplianceItem(Base):
    """合规项数据模型"""
    __tablename__ = "compliance_item"

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    assessment_plan_id = Column(
        UUID(as_uuid=True), 
        ForeignKey("assessment_plan.id", ondelete="CASCADE"), 
        nullable=False, 
        comment="关联的业务系统ID"
    )
    title = Column(Text, nullable=False, comment="标题")
    description = Column(Text, nullable=True, comment="描述")
    guide = Column(Text, nullable=True, comment="指导说明")
    inspector = Column(Text, nullable=True, comment="检查人")
    compliance_type = Column(
        Enum('risk', 'guarantee', name='compliance_type'), 
        nullable=False, 
        comment="合规类型（风险/保障）"
    )
    maturity_level = Column(Integer, nullable=True, default=1, comment="成熟度等级")
    risk_level = Column(
        Enum('very-high', 'high', 'medium', 'low', 'very-low', 'not-applicable', name='risk_level'), 
        nullable=True, 
        comment="风险级别"
    )
    compliance_status = Column(
        Enum('non-compliant', 'partially-compliant', 'fully-compliant', 'not-applicable', name='compliance_status'),
        nullable=False,
        default='non-compliant',
        comment="合规状态"
    )
    progress_status = Column(
        Enum('in-progress', 'remediation', 'completed', name='progress_status'),
        nullable=False,
        default='in-progress',
        comment="进度状态"
    )
    source_type = Column(
        Enum('system','user', name='source_type'), 
        nullable=True, 
        comment="风险级别"
    )
    updated_at = Column(
        DateTime(timezone=True), 
        default=datetime.now, 
        onupdate=datetime.now, 
        nullable=False, 
        comment="更新时间"
    )

    # 关系
    assessment_plan = relationship("AssessmentPlan", back_populates="compliance_items")
    issues = relationship("ComplianceItemIssue", back_populates="compliance_item", cascade="all, delete-orphan")
    attachments = relationship("Attachment", back_populates="compliance_item", cascade="all, delete-orphan")
    remediations = relationship("RemediationItem", back_populates="compliance_item", cascade="all, delete-orphan")